package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailCompanyDTO;
import com.dodoke.entity.RetailCompany;
import com.dodoke.service.IRetailCompanyService;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 公司信息控制层
 */
@Slf4j
@RestController
@RequestMapping("/retail/company")
public class RetailCompanyController {
    @Autowired
    private IRetailCompanyService retailCompanyService;

    /**
     * 公司信息分页查询
     *
     * @param currentPage      当前页码
     * @param pageSize         每页显示条数
     * @param retailCompanyDTO 模糊查询参数
     * @return
     */
    @PostMapping("getListByPage/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody RetailCompanyDTO retailCompanyDTO) {
        return AjaxResult.success("获取公司分页数据成功", retailCompanyService.getListByPage(currentPage, pageSize, retailCompanyDTO));
    }

    /**
     * 新增公司信息
     *
     * @param retailCompany 公司信息参数
     * @return
     */
    @PostMapping
    public AjaxResult addRetailCompany(@Validated @RequestBody RetailCompany retailCompany) {
        // 判断公司名称是否重复
        RetailCompany retailCompanyByName = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getName, retailCompany.getName()));
        if (retailCompanyByName != null) {
            return AjaxResult.error(HttpStatus.ERROR, "公司名称已存在");
        }
        // 判断公司编码是否重复
        if (retailCompany.getCompanyNo() != null && !retailCompany.getCompanyNo().isEmpty()) {
            RetailCompany retailCompanyByCompanyNo = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getCompanyNo, retailCompany.getCompanyNo()));
            if (retailCompanyByCompanyNo != null) {
                return AjaxResult.error(HttpStatus.ERROR, "公司编号已存在");
            }
        }
        // 判断公司别名是否重复
        RetailCompany retailCompanyByAlias = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getAlias, retailCompany.getAlias()));
        if (retailCompanyByAlias != null) {
            return AjaxResult.error(HttpStatus.ERROR, "公司别名已存在");
        }
        int count = retailCompanyService.addRetailCompany(retailCompany);
        if (count <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "新增公司信息失败");
        } else {
            return AjaxResult.error(HttpStatus.SUCCESS, "新增公司信息成功");
        }
    }

    /**
     * 修改公司信息
     *
     * @param retailCompany 公司信息参数
     * @return
     */
    @PutMapping
    public AjaxResult editRetailCompany(@Validated @RequestBody RetailCompany retailCompany) {
        RetailCompany retailCompanyById = retailCompanyService.getById(retailCompany.getId());
        // 判断公司名称是否重复
        RetailCompany retailCompanyByName = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getName, retailCompany.getName()));
        if (retailCompanyByName != null && !retailCompanyByName.getName().equals(retailCompanyById.getName())) {
            return AjaxResult.error(HttpStatus.ERROR, "公司名称已存在");
        }
        // 判断公司编码是否重复
        if (retailCompany.getCompanyNo() != null && !retailCompany.getCompanyNo().isEmpty()) {
            RetailCompany retailCompanyByCompanyNo = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getCompanyNo, retailCompany.getCompanyNo()));
            if (retailCompanyByCompanyNo != null && !retailCompanyByCompanyNo.getCompanyNo().equals(retailCompanyById.getCompanyNo())) {
                return AjaxResult.error(HttpStatus.ERROR, "公司编号已存在");
            }
        }
        // 判断公司别名是否重复
        RetailCompany retailCompanyByAlias = retailCompanyService.getOne(new LambdaQueryWrapper<RetailCompany>().eq(RetailCompany::getAlias, retailCompany.getAlias()));
        if (retailCompanyByAlias != null && !retailCompanyByAlias.getAlias().equals(retailCompanyById.getAlias())) {
            return AjaxResult.error(HttpStatus.ERROR, "公司别名已存在");
        }
        int count = retailCompanyService.editRetailCompany(retailCompany);
        if (count <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "修改公司信息失败");
        } else {
            return AjaxResult.error(HttpStatus.SUCCESS, "修改公司信息成功");
        }
    }

    /**
     * 根据id获取公司信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getRetailCompanyById(@PathVariable Long id) {
        return AjaxResult.success("获取公司信息成功", retailCompanyService.getRetailCompanyById(id));
    }

    /**
     * 根据id进行删除公司信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id) {
        boolean isRemove = retailCompanyService.removeById(id);
        return isRemove ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
